package com.ego.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ego.admin.dao.SysMenuDao;
import com.ego.admin.po.SysMenu;
import com.ego.admin.service.SysAdminRoleService;
import com.ego.admin.service.SysAdminService;
import com.ego.admin.service.SysMenuService;
import com.ego.admin.service.SysRoleMenuService;
import com.ego.common.constant.SystemConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: ego
 * @description:
 * @author: KamTang
 * @create: 2022-11-12 15:36
 **/
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {

    @Autowired
    private SysAdminService sysAdminService;
    @Autowired
    private SysAdminRoleService userRoleService;
    @Autowired
    private SysRoleMenuService roleMenuService;

    @Override
    public List<SysMenu> gets(Long parentId, List<Long> menuIdList) {
        List<SysMenu> menuList = gets(parentId);
        if(menuIdList == null){
            return menuList;
        }

        List<SysMenu> userMenuList = new ArrayList<>();
        for(SysMenu menu : menuList){
            if(menuIdList.contains(menu.getMenuId())){
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<SysMenu> gets(Long parentId) {
        return baseMapper.queryListParentId(parentId);
    }

    @Override
    public List<SysMenu> queryNotButtonList() {
        return baseMapper.queryNotButtonList();
    }

    @Override
    public List<SysMenu> getNavMenu(Long userId) {
        // 系统管理员，拥有最高权限
        if(userId == SystemConstant.UserEnum.SUPER_ADMIN.getCode()){
            return getAllMenuList(null);
        }

        // 用户菜单列表
        List<Long> menuIdList = sysAdminService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public void delete(Long menuId) {
        // 删除菜单
        this.removeById(menuId);
        // 删除菜单与角色关联
        roleMenuService.deleteByMenuId(menuId);
    }

    @Override
    public Set<String> getUserPermissions(long userId) {
        List<String> permsList;
        if (userId == SystemConstant.UserEnum.SUPER_ADMIN.getCode()) {
            List<SysMenu> menuList = this.baseMapper.selectList(null);
            permsList = new ArrayList<>(menuList.size());
            for (SysMenu menu : menuList
            ) {
                permsList.add(menu.getPerms());
            }
        } else {
            permsList = userRoleService.queryAllPermsByUserId(userId);
        }
        //用户权限列表
        Set<String> permsSet = new HashSet<>();
        for (String perms : permsList) {
            // 去掉空值
            if (StringUtils.isBlank(perms)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }


    /**
     * 获取所有菜单列表
     */
    private List<SysMenu> getAllMenuList(List<Long> menuIdList){
        //查询根菜单列表
        List<SysMenu> menuList = gets(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    /**
     * 递归
     */
    private List<SysMenu> getMenuTreeList(List<SysMenu> menuList, List<Long> menuIdList){
        List<SysMenu> subMenuList = new ArrayList<SysMenu>();

        for(SysMenu entity : menuList){
            // 目录
            if(entity.getType() == SystemConstant.MenuType.CATALOG.getCode()){
                entity.setList(getMenuTreeList(gets(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }
}
